package bancodedados;

import java.util.ArrayList;
import java.util.List;

import estruturas.Professor;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class ProfessorDAO {
	    private SQLiteDatabase db;
		private Helper helper;
		private String[] columns = {"matricula", "nome","endereco","telefone","senha"};
		
		public ProfessorDAO(Context context)
		{
			helper = new Helper(context);
		}
		
		public void open() throws SQLException
		{
			db = helper.getWritableDatabase();
		}
		
		public void close()
		{
			db.close();
		}

		public Professor addProfessor(String n)
		{
			ContentValues values = new ContentValues();
			values.put("nome", n);
			long insertMatricula = db.insert("cadastroProfessor", null, values);
			Cursor cursor = db.query("Professor", columns, null, null, null, null, null);
			
			cursor.moveToFirst();
			Professor newProfessor = cursorToProfessor(cursor);
			cursor.close();
			
			return newProfessor;
		}
		
		public List<String> getLista()
		{
			List<String> professor = new ArrayList<String>();
			
			Cursor cursor = db.query("Professor", columns, null, null, null, null, null);
			
			cursor.moveToFirst();
			while(!cursor.isAfterLast())
			{
				professor.add(cursorToProfessor(cursor).getNome());
				cursor.moveToNext();
			}
			
			cursor.close();
			return professor;
		}
		
		public boolean existeProfessor(String n){
			Cursor cursor = db.query("Professor", columns, "Lower(nome) = '" + n.toLowerCase() + "'", null, null, null, null);
			
			if (cursor.getCount()>0){
				cursor.close();
				return true;
			}else{
				cursor.close();
				return false;
			}
			
		}
		
		
		private Professor cursorToProfessor(Cursor cursor)
		{
			Professor professor = new Professor();
			
			professor.setMatricula(cursor.getInt(0));
			professor.setNome(cursor.getString(1));
			professor.setEndereco(cursor.getString(1));
			professor.setTelefone(cursor.getString(1));
			professor.setSenha(cursor.getString(1));
			return professor;
		}
	}



